import requests


class Actor:
    def __init__(self):
        self.session = requests.Session()
        # 获取验证码 必须要登录
        url = "http://api.51tickets.com/login"
        body = {"username": "s001", "password": "123456", "code": "1357", "uuid": "9c9d23417cf8406db2ea8916235d92d5"}
        resp = self.session.post(url=url, json=body)
        token = resp.json()["token"]

        headers = {"authorization": "Bearer " + token}
        self.session.headers.update(headers)

    # 根据ID查询 演员信息 返回值  接口返回什么 此函数的返回值 取它的JSON数据
    def search_actor_id(self, id):
        # 设置url地址
        url = "http://api.51tickets.com/system/actor/" + str(id)
        res = self.session.get(url)
        return res.json()

    # 根据演员的姓名查询演员信息
    def search_actor_name(self, pageNum, pageSize, actorName):
        # 设置url地址
        url = "http://api.51tickets.com/system/actor/list"
        # 设置参数
        if actorName == None:
            params = {"pageNum": pageNum, "pageSize": pageSize}
        else:
            params = {"pageNum": pageNum, "pageSize": pageSize, "actorName": actorName}
        # 发送请求
        res = self.session.get(url=url, params=params)
        return res.json()

    # 上传文件
    def upload_image(self, path):
        # 设置url地址
        url = "http://api.51tickets.com/common/upload"
        # 设置参数
        fp = open(path, mode="rb")
        files = {"file": fp}

        # 发送请求
        res = self.session.post(url=url, files=files)
        return res.json()

    # 保存演员
    def add_actor(self, actorName, description):
        # 设置url地址
        url = "http://api.51tickets.com/system/actor"
        # 设置参数
        body = {
            "actorId": None,
            "actorName": actorName,
            "actorImg": None,
            "actorDesc": description,
            "followers": None,
            "boxOffice": None
        }

        # 发送请求
        res = self.session.post(url=url, json=body)
        return res.json()

    # 更新演员信息
    def modify_actor(self, actorId, actorName, description):
        # 设置url地址
        url = "http://api.51tickets.com/system/actor"
        # 设置参数
        body = {
            "actorId": actorId,
            "actorName": actorName,
            "actorImg": None,
            "actorDesc": description,
            "followers": None,
            "boxOffice": None
        }

        # 发送请求
        res = self.session.put(url=url, json=body)
        return res.json()

    # 删除演员信息
    def remove_actor(self, actorId):
        # 设置url地址
        url = "http://api.51tickets.com/system/actor/" + str(actorId)
        res = self.session.delete(url)
        return res.json()


if __name__ == '__main__':
    actor = Actor()
    # result = actor.search_actor_id(126)
    # print(result)
    # result = actor.search_actor_name(1,10,"森七菜")
    # print(result)
    # result = actor.upload_image("../imgs/b.jpg")
    # print(result)

    # result = actor.add_actor("wugd","好人")
    # print(result)
    result = actor.modify_actor(126, "wugd", "非常好的人")
    print(result)
